package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.dao.PostageTemplateDao;
import cn.gjculture.shopping.api.entity.PostageTemplate;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.PostageTemplateService;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostageTemplateServiceImpl implements PostageTemplateService {

    @Autowired
    PostageTemplateDao postageTemplateDao;

    @Autowired
    CommonUpdateMapper commonUpdateMapper;

    @Override
    public int insert(PostageTemplate postageTemplate) {
        postageTemplate.setPostageTemplateId(UUIDUtil.getUUID());
        return postageTemplateDao.insert(postageTemplate);
    }

    @Override
    public int update(PostageTemplate postageTemplate) {
        return commonUpdateMapper.updatePostageTemplate(postageTemplate);
    }

    @Override
    public List<PostageTemplate> query(PostageTemplate template) {
        return postageTemplateDao.query();
    }

    @Override
    public PostageTemplate queryById(String id) {
        return postageTemplateDao.queryById(id);
    }

    @Override
    public int delete(String id, Return ret) {
        return 0;
    }

    @Override
    public int delete(String id, boolean withUsed, Return ret) {
        if(withUsed){
            postageTemplateDao.deleteMapGoods(id);
        }else{
            if(postageTemplateDao.queryCountMapGoodsById(id)>0){
                ret.getCommon().setMessage("模板已经被使用，不能删除");
                return 0;
            }
        }
        return postageTemplateDao.delete(id);
    }
}
